
var elementStyleStorages = [];
var elementClickStorages = [];
var floatElement = undefined;

function floatUp(element) {

    elementClickStorages[element] = element.onclick;

    var cssText = element.style.cssText;
    elementStyleStorages[element] = cssText;
    element.style.cssText = "-webkit-box-shadow: 2px 2px 10px #000000; background:#3DA2C0;" + cssText;

    element.onmouseout = function(ev) {
        ev.srcElement.style.cssText = elementStyleStorages[ev.srcElement];
        ev.srcElement.onclick = elementClickStorages[ev.srcElement];
    };

    element.onclick = function (ev) {
        var element = ev.srcElement; 
        console.log(element);

        var tagname = element.tagName;
        var classvalue = element.getAttribute("class");
        var namevalue = element.getAttribute("name");

        var keys = [];
        var key = {"tagname":tagname, "classvalue":classvalue, "namevalue":namevalue};

        getParentKey(element, keys);

        sendElementKeys(keys);
        return false;
    };
}

var floatUpTimer = undefined;
document.onmouseover = function (ev) {
    floatElement = ev.srcElement;
    if (floatUpTimer !== undefined)
        clearTimeout(floatUpTimer);
    floatUpTimer = setTimeout("floatUp(floatElement)", 500);
};

document.onmouseout = function (ev) {
    if (floatUpTimer !== undefined)
        clearTimeout(floatUpTimer);
};

function getParentKey(element, keys) {
    if (element === undefined)
        return;

    var tagname = element.tagName;

    if (tagname === "html" || tagname === "HTML")
        return;

    var classvalue = element.getAttribute("class");
    var namevalue = element.getAttribute("name");

    keys.push({"tagname":tagname, "classvalue":classvalue, "namevalue":namevalue});

    var count = 0;
    for (var i = 0; i < keys.length; i ++) {
        if (keys[i]['classvalue'] !== "" && keys[i]['classvalue'] !== undefined && keys[i]['classvalue'] !== null) {
            count += 1;
        }
    }

    if (count >= 4)
        return;

    getParentKey(element.parentNode, keys);
}

function sendElementKeys(keys) {
    chrome.extension.sendMessage({keys:keys}, function(response){});
}

chrome.extension.onMessage.addListener(
    function (request, sender, sendResponse) {
    }
);




